$(function(){
	function formatTitle(title, currentArray, currentIndex, currentOpts) {
		return '<div>' + (title && title.length ? '<b>' + title + '</b>' : '' ) + '</div>';
	}


	// Preview photo
	$('.preview').fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	500, 
		'speedOut'		:	200, 
		'overlayShow'	:	false,
		'titlePosition' :   'inside',
		'titleFormat'	:   formatTitle
	});

	// Show Alert bad announcement form
	$('.alertBadAnnouncementBtn').bind('click', function(){
		$(this).next('.alertBadAnnouncementForm').toggle();
	});

	// Alert bad announcement
	$('.alertBadAnnouncementForm .btnOk').bind('click', function(){
		var alertBadAnnouncementForm = $(this).parent();
		var announcementId = alertBadAnnouncementForm.children('.announcementId').val();
		var message = alertBadAnnouncementForm.children('.alertBadAnnouncementMessage').val();
		var loading = $('#message_' + announcementId);
		
		alertBadAnnouncementForm.hide();
		
		loading.html('<img src="' + IMAGE_URL + '/iconLoading.gif" />').show();
		
		$.post(ROOT_URL + '/alert/create', {"announcementId":announcementId,"message":message}, function(res){
			var res = JSON.parse(res);
			
			if (res.code == 'KO') {
				loading.css('color','red').html(res.description).show().delay(1000).fadeOut(1000);
			
			} else {
				loading.css('color','green').html(res.description).show().delay(1000).fadeOut(1000);
			}
		});
	});

});